using System;
using System.ComponentModel;
using System.Data;
using System.Threading;
using System.Windows.Forms;
using PCSComPayroll.BO;
using PCSComUtils.Common;
using PCSComUtils.PCSExc;
using PCSUtils.Log;
using PCSUtils.Utils;

namespace PCSPayroll.Payroll
{
	/// <summary>
	/// Summary description for CalculateProductionNorm.
	/// </summary>
	public class CalculateProductionNorm : Form
	{
		private PictureBox ptbImage;
		private Button btnClose;
		private Button btnHelp;
		private Button btnProcess;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private Container components = null;
		Thread thrProcess = null;
		
		private const string THIS = "PCSPayroll.Payroll.CalculateProductionNorm";
		private Button btnFGoodsCode;
		private TextBox txtFGoodsCode;
		private Label lblFGoodsCode;
		
		public CalculateProductionNorm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>t
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(CalculateProductionNorm));
			this.ptbImage = new System.Windows.Forms.PictureBox();
			this.btnFGoodsCode = new System.Windows.Forms.Button();
			this.txtFGoodsCode = new System.Windows.Forms.TextBox();
			this.btnClose = new System.Windows.Forms.Button();
			this.btnHelp = new System.Windows.Forms.Button();
			this.lblFGoodsCode = new System.Windows.Forms.Label();
			this.btnProcess = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// ptbImage
			// 
			this.ptbImage.AccessibleDescription = resources.GetString("ptbImage.AccessibleDescription");
			this.ptbImage.AccessibleName = resources.GetString("ptbImage.AccessibleName");
			this.ptbImage.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("ptbImage.Anchor")));
			this.ptbImage.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("ptbImage.BackgroundImage")));
			this.ptbImage.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.ptbImage.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("ptbImage.Dock")));
			this.ptbImage.Enabled = ((bool)(resources.GetObject("ptbImage.Enabled")));
			this.ptbImage.Font = ((System.Drawing.Font)(resources.GetObject("ptbImage.Font")));
			this.ptbImage.Image = ((System.Drawing.Image)(resources.GetObject("ptbImage.Image")));
			this.ptbImage.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("ptbImage.ImeMode")));
			this.ptbImage.Location = ((System.Drawing.Point)(resources.GetObject("ptbImage.Location")));
			this.ptbImage.Name = "ptbImage";
			this.ptbImage.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("ptbImage.RightToLeft")));
			this.ptbImage.Size = ((System.Drawing.Size)(resources.GetObject("ptbImage.Size")));
			this.ptbImage.SizeMode = ((System.Windows.Forms.PictureBoxSizeMode)(resources.GetObject("ptbImage.SizeMode")));
			this.ptbImage.TabIndex = ((int)(resources.GetObject("ptbImage.TabIndex")));
			this.ptbImage.TabStop = false;
			this.ptbImage.Text = resources.GetString("ptbImage.Text");
			this.ptbImage.Visible = ((bool)(resources.GetObject("ptbImage.Visible")));
			// 
			// btnFGoodsCode
			// 
			this.btnFGoodsCode.AccessibleDescription = resources.GetString("btnFGoodsCode.AccessibleDescription");
			this.btnFGoodsCode.AccessibleName = resources.GetString("btnFGoodsCode.AccessibleName");
			this.btnFGoodsCode.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("btnFGoodsCode.Anchor")));
			this.btnFGoodsCode.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btnFGoodsCode.BackgroundImage")));
			this.btnFGoodsCode.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("btnFGoodsCode.Dock")));
			this.btnFGoodsCode.Enabled = ((bool)(resources.GetObject("btnFGoodsCode.Enabled")));
			this.btnFGoodsCode.FlatStyle = ((System.Windows.Forms.FlatStyle)(resources.GetObject("btnFGoodsCode.FlatStyle")));
			this.btnFGoodsCode.Font = ((System.Drawing.Font)(resources.GetObject("btnFGoodsCode.Font")));
			this.btnFGoodsCode.Image = ((System.Drawing.Image)(resources.GetObject("btnFGoodsCode.Image")));
			this.btnFGoodsCode.ImageAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("btnFGoodsCode.ImageAlign")));
			this.btnFGoodsCode.ImageIndex = ((int)(resources.GetObject("btnFGoodsCode.ImageIndex")));
			this.btnFGoodsCode.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("btnFGoodsCode.ImeMode")));
			this.btnFGoodsCode.Location = ((System.Drawing.Point)(resources.GetObject("btnFGoodsCode.Location")));
			this.btnFGoodsCode.Name = "btnFGoodsCode";
			this.btnFGoodsCode.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("btnFGoodsCode.RightToLeft")));
			this.btnFGoodsCode.Size = ((System.Drawing.Size)(resources.GetObject("btnFGoodsCode.Size")));
			this.btnFGoodsCode.TabIndex = ((int)(resources.GetObject("btnFGoodsCode.TabIndex")));
			this.btnFGoodsCode.Text = resources.GetString("btnFGoodsCode.Text");
			this.btnFGoodsCode.TextAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("btnFGoodsCode.TextAlign")));
			this.btnFGoodsCode.Visible = ((bool)(resources.GetObject("btnFGoodsCode.Visible")));
			this.btnFGoodsCode.Click += new System.EventHandler(this.btnFGoodsCode_Click);
			// 
			// txtFGoodsCode
			// 
			this.txtFGoodsCode.AccessibleDescription = resources.GetString("txtFGoodsCode.AccessibleDescription");
			this.txtFGoodsCode.AccessibleName = resources.GetString("txtFGoodsCode.AccessibleName");
			this.txtFGoodsCode.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("txtFGoodsCode.Anchor")));
			this.txtFGoodsCode.AutoSize = ((bool)(resources.GetObject("txtFGoodsCode.AutoSize")));
			this.txtFGoodsCode.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("txtFGoodsCode.BackgroundImage")));
			this.txtFGoodsCode.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("txtFGoodsCode.Dock")));
			this.txtFGoodsCode.Enabled = ((bool)(resources.GetObject("txtFGoodsCode.Enabled")));
			this.txtFGoodsCode.Font = ((System.Drawing.Font)(resources.GetObject("txtFGoodsCode.Font")));
			this.txtFGoodsCode.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("txtFGoodsCode.ImeMode")));
			this.txtFGoodsCode.Location = ((System.Drawing.Point)(resources.GetObject("txtFGoodsCode.Location")));
			this.txtFGoodsCode.MaxLength = ((int)(resources.GetObject("txtFGoodsCode.MaxLength")));
			this.txtFGoodsCode.Multiline = ((bool)(resources.GetObject("txtFGoodsCode.Multiline")));
			this.txtFGoodsCode.Name = "txtFGoodsCode";
			this.txtFGoodsCode.PasswordChar = ((char)(resources.GetObject("txtFGoodsCode.PasswordChar")));
			this.txtFGoodsCode.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("txtFGoodsCode.RightToLeft")));
			this.txtFGoodsCode.ScrollBars = ((System.Windows.Forms.ScrollBars)(resources.GetObject("txtFGoodsCode.ScrollBars")));
			this.txtFGoodsCode.Size = ((System.Drawing.Size)(resources.GetObject("txtFGoodsCode.Size")));
			this.txtFGoodsCode.TabIndex = ((int)(resources.GetObject("txtFGoodsCode.TabIndex")));
			this.txtFGoodsCode.Text = resources.GetString("txtFGoodsCode.Text");
			this.txtFGoodsCode.TextAlign = ((System.Windows.Forms.HorizontalAlignment)(resources.GetObject("txtFGoodsCode.TextAlign")));
			this.txtFGoodsCode.Visible = ((bool)(resources.GetObject("txtFGoodsCode.Visible")));
			this.txtFGoodsCode.WordWrap = ((bool)(resources.GetObject("txtFGoodsCode.WordWrap")));
			this.txtFGoodsCode.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtFGoodsCode_KeyDown);
			this.txtFGoodsCode.Validating += new System.ComponentModel.CancelEventHandler(this.txtFGoodsCode_Validating);
			// 
			// btnClose
			// 
			this.btnClose.AccessibleDescription = resources.GetString("btnClose.AccessibleDescription");
			this.btnClose.AccessibleName = resources.GetString("btnClose.AccessibleName");
			this.btnClose.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("btnClose.Anchor")));
			this.btnClose.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btnClose.BackgroundImage")));
			this.btnClose.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.btnClose.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("btnClose.Dock")));
			this.btnClose.Enabled = ((bool)(resources.GetObject("btnClose.Enabled")));
			this.btnClose.FlatStyle = ((System.Windows.Forms.FlatStyle)(resources.GetObject("btnClose.FlatStyle")));
			this.btnClose.Font = ((System.Drawing.Font)(resources.GetObject("btnClose.Font")));
			this.btnClose.Image = ((System.Drawing.Image)(resources.GetObject("btnClose.Image")));
			this.btnClose.ImageAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("btnClose.ImageAlign")));
			this.btnClose.ImageIndex = ((int)(resources.GetObject("btnClose.ImageIndex")));
			this.btnClose.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("btnClose.ImeMode")));
			this.btnClose.Location = ((System.Drawing.Point)(resources.GetObject("btnClose.Location")));
			this.btnClose.Name = "btnClose";
			this.btnClose.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("btnClose.RightToLeft")));
			this.btnClose.Size = ((System.Drawing.Size)(resources.GetObject("btnClose.Size")));
			this.btnClose.TabIndex = ((int)(resources.GetObject("btnClose.TabIndex")));
			this.btnClose.Text = resources.GetString("btnClose.Text");
			this.btnClose.TextAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("btnClose.TextAlign")));
			this.btnClose.Visible = ((bool)(resources.GetObject("btnClose.Visible")));
			this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
			// 
			// btnHelp
			// 
			this.btnHelp.AccessibleDescription = resources.GetString("btnHelp.AccessibleDescription");
			this.btnHelp.AccessibleName = resources.GetString("btnHelp.AccessibleName");
			this.btnHelp.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("btnHelp.Anchor")));
			this.btnHelp.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btnHelp.BackgroundImage")));
			this.btnHelp.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("btnHelp.Dock")));
			this.btnHelp.Enabled = ((bool)(resources.GetObject("btnHelp.Enabled")));
			this.btnHelp.FlatStyle = ((System.Windows.Forms.FlatStyle)(resources.GetObject("btnHelp.FlatStyle")));
			this.btnHelp.Font = ((System.Drawing.Font)(resources.GetObject("btnHelp.Font")));
			this.btnHelp.Image = ((System.Drawing.Image)(resources.GetObject("btnHelp.Image")));
			this.btnHelp.ImageAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("btnHelp.ImageAlign")));
			this.btnHelp.ImageIndex = ((int)(resources.GetObject("btnHelp.ImageIndex")));
			this.btnHelp.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("btnHelp.ImeMode")));
			this.btnHelp.Location = ((System.Drawing.Point)(resources.GetObject("btnHelp.Location")));
			this.btnHelp.Name = "btnHelp";
			this.btnHelp.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("btnHelp.RightToLeft")));
			this.btnHelp.Size = ((System.Drawing.Size)(resources.GetObject("btnHelp.Size")));
			this.btnHelp.TabIndex = ((int)(resources.GetObject("btnHelp.TabIndex")));
			this.btnHelp.Text = resources.GetString("btnHelp.Text");
			this.btnHelp.TextAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("btnHelp.TextAlign")));
			this.btnHelp.Visible = ((bool)(resources.GetObject("btnHelp.Visible")));
			// 
			// lblFGoodsCode
			// 
			this.lblFGoodsCode.AccessibleDescription = resources.GetString("lblFGoodsCode.AccessibleDescription");
			this.lblFGoodsCode.AccessibleName = resources.GetString("lblFGoodsCode.AccessibleName");
			this.lblFGoodsCode.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("lblFGoodsCode.Anchor")));
			this.lblFGoodsCode.AutoSize = ((bool)(resources.GetObject("lblFGoodsCode.AutoSize")));
			this.lblFGoodsCode.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("lblFGoodsCode.Dock")));
			this.lblFGoodsCode.Enabled = ((bool)(resources.GetObject("lblFGoodsCode.Enabled")));
			this.lblFGoodsCode.Font = ((System.Drawing.Font)(resources.GetObject("lblFGoodsCode.Font")));
			this.lblFGoodsCode.ForeColor = System.Drawing.Color.Maroon;
			this.lblFGoodsCode.Image = ((System.Drawing.Image)(resources.GetObject("lblFGoodsCode.Image")));
			this.lblFGoodsCode.ImageAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("lblFGoodsCode.ImageAlign")));
			this.lblFGoodsCode.ImageIndex = ((int)(resources.GetObject("lblFGoodsCode.ImageIndex")));
			this.lblFGoodsCode.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("lblFGoodsCode.ImeMode")));
			this.lblFGoodsCode.Location = ((System.Drawing.Point)(resources.GetObject("lblFGoodsCode.Location")));
			this.lblFGoodsCode.Name = "lblFGoodsCode";
			this.lblFGoodsCode.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("lblFGoodsCode.RightToLeft")));
			this.lblFGoodsCode.Size = ((System.Drawing.Size)(resources.GetObject("lblFGoodsCode.Size")));
			this.lblFGoodsCode.TabIndex = ((int)(resources.GetObject("lblFGoodsCode.TabIndex")));
			this.lblFGoodsCode.Text = resources.GetString("lblFGoodsCode.Text");
			this.lblFGoodsCode.TextAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("lblFGoodsCode.TextAlign")));
			this.lblFGoodsCode.Visible = ((bool)(resources.GetObject("lblFGoodsCode.Visible")));
			// 
			// btnProcess
			// 
			this.btnProcess.AccessibleDescription = resources.GetString("btnProcess.AccessibleDescription");
			this.btnProcess.AccessibleName = resources.GetString("btnProcess.AccessibleName");
			this.btnProcess.Anchor = ((System.Windows.Forms.AnchorStyles)(resources.GetObject("btnProcess.Anchor")));
			this.btnProcess.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("btnProcess.BackgroundImage")));
			this.btnProcess.Dock = ((System.Windows.Forms.DockStyle)(resources.GetObject("btnProcess.Dock")));
			this.btnProcess.Enabled = ((bool)(resources.GetObject("btnProcess.Enabled")));
			this.btnProcess.FlatStyle = ((System.Windows.Forms.FlatStyle)(resources.GetObject("btnProcess.FlatStyle")));
			this.btnProcess.Font = ((System.Drawing.Font)(resources.GetObject("btnProcess.Font")));
			this.btnProcess.Image = ((System.Drawing.Image)(resources.GetObject("btnProcess.Image")));
			this.btnProcess.ImageAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("btnProcess.ImageAlign")));
			this.btnProcess.ImageIndex = ((int)(resources.GetObject("btnProcess.ImageIndex")));
			this.btnProcess.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("btnProcess.ImeMode")));
			this.btnProcess.Location = ((System.Drawing.Point)(resources.GetObject("btnProcess.Location")));
			this.btnProcess.Name = "btnProcess";
			this.btnProcess.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("btnProcess.RightToLeft")));
			this.btnProcess.Size = ((System.Drawing.Size)(resources.GetObject("btnProcess.Size")));
			this.btnProcess.TabIndex = ((int)(resources.GetObject("btnProcess.TabIndex")));
			this.btnProcess.Text = resources.GetString("btnProcess.Text");
			this.btnProcess.TextAlign = ((System.Drawing.ContentAlignment)(resources.GetObject("btnProcess.TextAlign")));
			this.btnProcess.Visible = ((bool)(resources.GetObject("btnProcess.Visible")));
			this.btnProcess.Click += new System.EventHandler(this.btnProcess_Click);
			// 
			// CalculateProductionNorm
			// 
			this.AccessibleDescription = resources.GetString("$this.AccessibleDescription");
			this.AccessibleName = resources.GetString("$this.AccessibleName");
			this.AutoScaleBaseSize = ((System.Drawing.Size)(resources.GetObject("$this.AutoScaleBaseSize")));
			this.AutoScroll = ((bool)(resources.GetObject("$this.AutoScroll")));
			this.AutoScrollMargin = ((System.Drawing.Size)(resources.GetObject("$this.AutoScrollMargin")));
			this.AutoScrollMinSize = ((System.Drawing.Size)(resources.GetObject("$this.AutoScrollMinSize")));
			this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
			this.CancelButton = this.btnClose;
			this.ClientSize = ((System.Drawing.Size)(resources.GetObject("$this.ClientSize")));
			this.Controls.Add(this.ptbImage);
			this.Controls.Add(this.btnFGoodsCode);
			this.Controls.Add(this.txtFGoodsCode);
			this.Controls.Add(this.btnClose);
			this.Controls.Add(this.btnHelp);
			this.Controls.Add(this.lblFGoodsCode);
			this.Controls.Add(this.btnProcess);
			this.Enabled = ((bool)(resources.GetObject("$this.Enabled")));
			this.Font = ((System.Drawing.Font)(resources.GetObject("$this.Font")));
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.ImeMode = ((System.Windows.Forms.ImeMode)(resources.GetObject("$this.ImeMode")));
			this.Location = ((System.Drawing.Point)(resources.GetObject("$this.Location")));
			this.MaximizeBox = false;
			this.MaximumSize = ((System.Drawing.Size)(resources.GetObject("$this.MaximumSize")));
			this.MinimumSize = ((System.Drawing.Size)(resources.GetObject("$this.MinimumSize")));
			this.Name = "CalculateProductionNorm";
			this.RightToLeft = ((System.Windows.Forms.RightToLeft)(resources.GetObject("$this.RightToLeft")));
			this.StartPosition = ((System.Windows.Forms.FormStartPosition)(resources.GetObject("$this.StartPosition")));
			this.Text = resources.GetString("$this.Text");
			this.Closing += new System.ComponentModel.CancelEventHandler(this.CalculateProductionNorm_Closing);
			this.Load += new System.EventHandler(this.CalculateProductionNorm_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void CalculateProductionNorm_Load(object sender, EventArgs e)
		{
			const string METHOD_NAME = THIS + ".CalculateProductionNorm_Load()";
			try
			{
				Security objSecurity = new Security();
				this.Name = THIS;
				if (objSecurity.SetRightForUserOnForm(this, SystemProperty.UserName) == 0)
				{
					this.Close();
					return;
				}

				txtFGoodsCode.Text = string.Empty;
			}
			catch (PCSException ex)
			{
				PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
				try
				{
					Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
				}
				catch
				{
					PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
				}
			}
			catch (Exception ex)
			{
				PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
				try
				{
					Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
				}
				catch
				{
					PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
				}
			}
		}

		private void txtFGoodsCode_Validating(object sender, CancelEventArgs e)
		{
			const string METHOD_NAME = THIS + ".txtFGoodsCode_Validating()";
			try
			{
				if (txtFGoodsCode.Modified)
				{
					if (txtFGoodsCode.Text != string.Empty)
						btnFGoodsCode_Click(sender, e);
					else
					{
						txtFGoodsCode.Tag = null;
						btnFGoodsCode.Tag = null;
						btnProcess.Enabled = false;
					}
				}
			}
			catch (PCSException ex)
			{
				PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
				try
				{
					Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
				}
				catch
				{
					PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
				}
			}
			catch (Exception ex)
			{
				PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
				try
				{
					Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
				}
				catch
				{
					PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
				}
			}
		}

		private void txtFGoodsCode_KeyDown(object sender, KeyEventArgs e)
		{
			const string METHOD_NAME = THIS + ".txtFGoodsCode_KeyDown()";
			try
			{
				if (e.KeyCode == Keys.F4)
					btnFGoodsCode_Click(null, null);
			}
			catch (PCSException ex)
			{
				PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
				try
				{
					Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
				}
				catch
				{
					PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
				}
			}
			catch (Exception ex)
			{
				PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
				try
				{
					Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
				}
				catch
				{
					PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
				}
			}
		}

		private void btnFGoodsCode_Click(object sender, EventArgs e)
		{
			const string METHOD_NAME = THIS + ".btnFGoodsCode_Click()";
			try
			{
				DataRowView drwResult = null;
				string strWhere = SO_SaleOrderDetailTable.TABLE_NAME + "." + SO_SaleOrderDetailTable.SALEORDERDETAILID_FLD
					+ " NOT IN (SELECT " + PAY_EstimateMasterTable.TABLE_NAME + "." + PAY_EstimateMasterTable.SALEORDERDETAILID_FLD
					+ " FROM " + PAY_EstimateMasterTable.TABLE_NAME + ")";
				drwResult = FormControlComponents.OpenSearchForm(SO_SaleOrderDetailTable.TABLE_NAME, SO_SaleOrderDetailTable.FGOODSCODE_FLD, txtFGoodsCode.Text, strWhere);
				if (drwResult != null)
				{
					txtFGoodsCode.Text = drwResult[SO_SaleOrderDetailTable.FGOODSCODE_FLD].ToString();
					txtFGoodsCode.Tag = Convert.ToInt32(drwResult[SO_SaleOrderDetailTable.SALEORDERDETAILID_FLD]);
					btnFGoodsCode.Tag = Convert.ToInt32(drwResult[SO_SaleOrderDetailTable.PRODUCTID_FLD]);
					btnProcess.Enabled = true;
				}
				else
				{
					txtFGoodsCode.Focus();
					txtFGoodsCode.SelectAll();
				}
			}
			catch (PCSException ex)
			{
				PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
				try
				{
					Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
				}
				catch
				{
					PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
				}
			}
			catch (Exception ex)
			{
				PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
				try
				{
					Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
				}
				catch
				{
					PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
				}
			}
		}
		private void btnProcess_Click(object sender, EventArgs e)
		{
			const string METHOD_NAME = THIS + ".btnProcess_Click()";
			try
			{
				if (FormControlComponents.CheckMandatory(txtFGoodsCode))
				{
					PCSMessageBox.Show(ErrorCode.MANDATORY_INVALID, MessageBoxIcon.Warning);
					txtFGoodsCode.Focus();
					return;
				}
				SwitchMode(true);
                CalculateNorm();
                //thrProcess = new Thread(new ThreadStart(CalculateNorm));
                //thrProcess.CurrentCulture = Thread.CurrentThread.CurrentCulture;
                //thrProcess.CurrentUICulture = Thread.CurrentThread.CurrentUICulture;
                //thrProcess.Start();
                //if (thrProcess.ThreadState == ThreadState.Stopped || !thrProcess.IsAlive)
                //    thrProcess.Abort();
			}
			catch (ThreadAbortException ex)
			{
				Logger.LogMessage(ex, METHOD_NAME, Level.DEBUG);
			}
			catch (PCSException ex)
			{
				PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
				try
				{
					Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
				}
				catch
				{
					PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
				}
			}
			catch (Exception ex)
			{
				PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxIcon.Error);
				try
				{
					Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
				}
				catch
				{
					PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxIcon.Error);
				}
			}
		}

		private void CalculateNorm()
		{
			const string METHOD_NAME = THIS + ".CalculateNorm()";
			try
			{
				this.Cursor = Cursors.WaitCursor;
				CalculateNorm(Convert.ToInt32(txtFGoodsCode.Tag));
				string[] strMsg = new string[]{this.Text};
				PCSMessageBox.Show(ErrorCode.MESSAGE_TASK_COMPLETED, MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, strMsg);
				this.Cursor = Cursors.Default;
				SwitchMode(false);
			}
			catch (ThreadAbortException ex)
			{
				Logger.LogMessage(ex.Message, METHOD_NAME, Level.DEBUG);
			}
			catch (PCSException ex)
			{
				if (ex.mCode == ErrorCode.MESSAGE_CAN_NOT_DELETE)
				{
					string[] strMsg = new string[]{this.Text};
					PCSMessageBox.Show(ErrorCode.MESSAGE_CAN_NOT_DELETE, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, strMsg);
				}
				else
					PCSMessageBox.Show(ex.mCode, MessageBoxIcon.Error);
				try
				{
					Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
				}
				catch
				{
					PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
				}
			}
			catch (Exception ex)
			{
				string[] strMsg = new string[]{this.Text};
				PCSMessageBox.Show(ErrorCode.MESSAGE_CANNOT_ROLL_UP, MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1, strMsg);
				try
				{
					Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
				}
				catch
				{
					PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION);
				}
			}
			finally
			{
				this.Cursor = Cursors.Default;
				SwitchMode(false);
			}
		}
		private void CalculateNorm(int pintSaleOrderDetailID)
		{
			#region production norm table schema
			DataTable dtbProductionNorm = new DataTable(PAY_ProductionNormTable.TABLE_NAME);
			dtbProductionNorm.Columns.Add(new DataColumn(PAY_ProductionNormTable.PRODUCTIONNORMID_FLD, typeof(int)));
			dtbProductionNorm.Columns[PAY_ProductionNormTable.PRODUCTIONNORMID_FLD].AutoIncrement = true;
			dtbProductionNorm.Columns[PAY_ProductionNormTable.PRODUCTIONNORMID_FLD].AutoIncrementSeed = 1;
			dtbProductionNorm.Columns[PAY_ProductionNormTable.PRODUCTIONNORMID_FLD].AutoIncrementStep = 1;
			dtbProductionNorm.Columns.Add(new DataColumn(PAY_ProductionNormTable.PRODUCTIONLINEID_FLD, typeof(int)));
			dtbProductionNorm.Columns.Add(new DataColumn(PAY_ProductionNormTable.SALEORDERDETAILID_FLD, typeof(int)));
			dtbProductionNorm.Columns[PAY_ProductionNormTable.SALEORDERDETAILID_FLD].DefaultValue = pintSaleOrderDetailID;
			dtbProductionNorm.Columns.Add(new DataColumn(PAY_ProductionNormTable.USERID_FLD, typeof(int)));
			dtbProductionNorm.Columns[PAY_ProductionNormTable.USERID_FLD].DefaultValue = SystemProperty.UserID;
			dtbProductionNorm.Columns.Add(new DataColumn(PAY_ProductionNormTable.TOTALTIME_FLD, typeof(decimal)));
			#endregion

			CalculateProductionNormBO boCalculate = new CalculateProductionNormBO();

			#region calculate production norm
			
			DataTable dtbRunTime = boCalculate.GetRunTime(pintSaleOrderDetailID);
			foreach (DataRow drowData in dtbRunTime.Rows)
			{
				decimal decTotalTime = 0;
				try
				{
					decTotalTime = Convert.ToDecimal(drowData[PRO_WORoutingTable.LABORRUNTIME_FLD]);
				}
				catch{}
				// add result
				DataRow drowNorm = dtbProductionNorm.NewRow();
				drowNorm[PAY_ProductionNormTable.PRODUCTIONLINEID_FLD] = drowData[PRO_WorkOrderMasterTable.PRODUCTIONLINEID_FLD];
				drowNorm[PAY_ProductionNormTable.TOTALTIME_FLD] = decTotalTime;
				dtbProductionNorm.Rows.Add(drowNorm);
			}
			
			#endregion

			// update to database
			DataSet dstData = new DataSet();
			dstData.Tables.Add(dtbProductionNorm);
			boCalculate.UpdateDataSet(dstData, pintSaleOrderDetailID.ToString());
		}
		
		private void SwitchMode(bool pblnProcessing)
		{
			txtFGoodsCode.Enabled = !pblnProcessing;
			btnProcess.Enabled = !pblnProcessing;
			ptbImage.Visible = pblnProcessing;
		}

		private void btnClose_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void CalculateProductionNorm_Closing(object sender, CancelEventArgs e)
		{
			const string METHOD_NAME = THIS + ".CalculateProductionNorm_Closing()";
			try
			{
				// ask user to stop the thread
				if (thrProcess != null)
				{
					if (thrProcess.IsAlive || thrProcess.ThreadState == ThreadState.Running)
					{
						string[] strMsg = {this.Text};					
						DialogResult dlgResult = PCSMessageBox.Show(ErrorCode.MESSAGE_PROCESS_IS_RUNNING, MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, strMsg);
						switch (dlgResult)
						{
							case DialogResult.OK:
								// try to stop the thread
								try
								{
									thrProcess.Abort();
								}
								catch
								{
									e.Cancel = false;
								}
								break;
							case DialogResult.Cancel:
								e.Cancel = true;
								break;
						}
					}
				}
			}
			catch (ThreadAbortException ex)
			{
				Logger.LogMessage(ex.Message, METHOD_NAME, Level.DEBUG);
			}
			catch (PCSException ex)
			{
				// displays the error message.
				PCSMessageBox.Show(ex.mCode, MessageBoxButtons.OK, MessageBoxIcon.Error);
				// log message.
				try
				{
					Logger.LogMessage(ex.CauseException, METHOD_NAME, Level.ERROR);
				}
				catch
				{
					PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
			catch (Exception ex)
			{
				// displays the error message.
				PCSMessageBox.Show(ErrorCode.OTHER_ERROR, MessageBoxButtons.OK, MessageBoxIcon.Error);
				// log message.
				try
				{
					Logger.LogMessage(ex, METHOD_NAME, Level.ERROR);
				}
				catch
				{
					PCSMessageBox.Show(ErrorCode.LOG_EXCEPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
		}

	}
}
